﻿/*======================================================================
== Copyright : BlueCurve (c)
== Licence   : Gnu/GPL v2.x
== Author    : Teddy Albina
== Email     : bluecurveteam@gmail.com
== Web site  : http://www.codeplex.com/BlueCurve
========================================================================*/
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
using BlueCurve.Common.Application;
using Indexer.Remoting;


namespace Indexer
{
    class Program : BlueCurve.Common.InteractiveConsole.BaseConsole
    {
        #region 'Configuration'

        /// <summary>
        /// Objet pour l'indexation des documents
        /// </summary>
        private static Indexation s_index= new Indexation();
        /// <summary>
        /// Thread de la console
        /// </summary>
        private static Thread s_console;
        /// <summary>
        /// Thread de traitement
        /// </summary>
        private static Thread s_treatment;
        /// <summary>
        /// Objet pour le traitement des données téléchargés
        /// </summary>
        private static DataTreatment treatment = new DataTreatment(); //DataTreatment.GetInstance;

        #endregion



        #region 'Function'

        static void Main(string[] args)
        {
            s_console = new Thread(new ThreadStart(new Program().Run));
            s_treatment = new Thread(new ThreadStart(treatment.RunTreatment));
            s_console.Start();
            s_treatment.Start();

            s_index.GetFilesToIndex += new EventHandler(index_GetFilesToIndex);
            s_index.RunIndexation();
        }
               

        /// <summary>
        /// Demande de nouveaux fichiers à indexer au repository
        /// et les ajoutent à la base de données
        /// </summary>
        private static void index_GetFilesToIndex(object sender, EventArgs e)
        {
            Tcpclient client = new Tcpclient();
            List<string> datas = client.GetFilesToIndex();

            if (datas != null)
            {
                using (IndexerdbDataContext db = new IndexerdbDataContext())
                {
                    datas.ForEach(
                        delegate(string path)
                        {
                            db.AddPath(path);
                        });
                }
            }
        }


        /// <summary>
        /// Arrete l'application
        /// </summary>
        private static void StopApplication()
        {
            treatment.IsStop = false;
            while (treatment.AliveThreads > 0) { Thread.Sleep(150); }

            s_index.IsStop = false;
            while (s_index.AliveThreads > 0) { Thread.Sleep(150); }

            // Suicide de l'application
            Process.GetCurrentProcess().Kill();
        }


        /// <summary>
        /// Redémarre l'application
        /// </summary>
        private static void RestartApplication()
        {
            treatment.IsStop = false;
            while (treatment.AliveThreads > 0) { Thread.Sleep(150); }

            s_index.IsStop = false;
            while (s_index.AliveThreads > 0) { Thread.Sleep(150); }

            // Enregistre l'application pour le redemarrage
            Restart.RestartApplication(string.Concat(System.Diagnostics.Process.GetCurrentProcess().ProcessName, ".exe"));
            // Suicide de l'application
            Process.GetCurrentProcess().Kill();

        }

        #endregion



        #region 'Console'

        /// <summary>
        /// Chargement des commandes de la console interactive
        /// </summary>
        protected override void LoadCommands()
        {
            this.Commands.Add("-stop", new Dictionary<Action, string>() 
            { 
                { () => StopApplication(), "Arrete l'application" }
            });
            this.Commands.Add("-restart", new Dictionary<Action, string>()
            {
                { () => RestartApplication(), "Redemarre l'application" }
            });
        }

        /// <summary>
        /// Retourne le nom de l'application
        /// </summary>
        public override string Name
        {
            get { return "BlueCurve Indexer"; }
        }

        #endregion
    }
}
